var app = getApp();
var Zan = require('../../../dist/index');
var Util = require('../../../utils/util.js');

Page(Object.assign({}, Zan.TopTips, {
	data: {
		title: '',
		subtitle: '加载中...',
		type: 'order/list',
		loading: true,
		hasMore: true,
		page: 1,
		size: 20,
		records: []
	},

	loadMore: function () {
		var that = this;

		if (!this.data.hasMore) return;

		this.setData({subtitle: '加载中...', loading: true});

		return app.battery.find(this.data.type, this.data.page, this.data.size).then(function (d) {
			if (d.list && d.list.length) {
				var _records = [];
				for (var i in d.list) {
					var leaseTime = d.list[i].leaseTime ? new Date(d.list[i].leaseTime) : '';
					var oldBatteryPutInTime = oldBatteryPutInTime ? new Date(d.list[i].oldBatteryPutInTime) : '';
					_records.push({
						id: d.list[i].id,
						batteryNumber: d.list[i].batteryNumber,
						sn: d.list[i].number,
						startTime: leaseTime ? Util.formatDate(leaseTime, 'yyyy-MM-dd hh:mm') : '-',
						endTime: oldBatteryPutInTime ? Util.formatDate(oldBatteryPutInTime, 'yyyy-MM-dd hh:mm') : '-',
						leaseHour: d.list[i].leaseHour,
						leaseMoney: d.list[i].leaseMoney,
						dl: d.list[i].curPower
					});
				}
				if (d.list.length < that.data.size) {
					that.setData({
						subtitle: '没有更多了',
						records: that.data.records.concat(_records),
						page: ++that.data.page,
						hasMore: false,
						loading: false
					});
				} else {
					that.setData({
						subtitle: '下拉加载更多',
						page: ++that.data.page,
						records: that.data.records.concat(_records),
						loading: false
					});
				}
			} else {
				that.setData({
					subtitle: '没有更多了',
					records: that.data.records,
					page: that.data.page,
					hasMore: false,
					loading: false
				});
			}
		}).catch(function (e) {
			// that.setData({ subtitle: '获取数据异常', loading: false });
			console.error(e);
		});
	},

	onPullDownRefresh: function () {
		this.setData({records: [], page: 1, hasMore: true});
		this.loadMore().then(function () {
			return app.wechat.original.stopPullDownRefresh();
		});
	},

	onReachBottom: function () {
		this.loadMore();
	},

	onLoad: function (options) {

	},

	showTopTips: function (msg) {
		this.showZanTopTips(msg);
	},

	onShow: function () {
		this.loadMore();
	},

	goToMap: function (e) {
		let item = e.currentTarget.dataset.item;
		wx.navigateTo({ url: "../../map/index?id=" + item.id });
	},

	onClickChargeDetail: function (e) {
		console.log(111)
	}

}));